package com.mz.ozy.activity.assets;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.mz.ozy.R;
import com.mz.ozy.activity.home.HomeActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.mvp.presenter.ScanPresenter;
import com.mz.ozy.mvp.view.ScanContract;
import com.mz.ozy.utils.ToastUtils;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.yancy.imageselector.ImageSelector;
import com.yancy.imageselector.ImageSelectorActivity;

import org.json.JSONObject;

import java.util.List;


/*
 * @Date 2019/6/1
 * @Des  扫一扫
 */
public class ScanHomeActivity extends BaseActivity<ScanContract.View, ScanPresenter> implements CodeUtils.AnalyzeCallback, View.OnClickListener, ScanContract.View {

    private static final int REQUEST_IMAGE = 1;
    private String username;
    CaptureFragment captureFragment;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        initTitle();
        tvTitle.setVisibility(View.GONE);
        tvCenterTitle.setText(R.string.qrcode);
        rightTextView.setText(R.string.pay_code);
        rightTextView.setOnClickListener(this);
        code = getIntent().getStringExtra("code");
        /**
         * 执行扫面Fragment的初始化操作
         */
        captureFragment = new CaptureFragment();
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);

        captureFragment.setAnalyzeCallback(this);
        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();


    }

    @Override
    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
        if (!TextUtils.isEmpty(result)) {

            goActivity(result);


        } else {
            ToastUtils.showToast(getString(R.string.invalid_QR_code));
            toFinish();


        }


    }

    @Override
    public void onAnalyzeFailed() {
        ToastUtils.showToast(getString(R.string.invalid_QR_code));
        toFinish();


    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, PaymentActivity.class);
        intent.putExtra("userName", "");
        intent.putExtra("code", code);
        startActivity(intent);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ImageSelector.IMAGE_REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            // Get Image Path List
            List<String> list = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);
            if (list != null && list.size() > 0) {
                CodeUtils.analyzeBitmap(list.get(0), new CodeUtils.AnalyzeCallback() {
                    @Override
                    public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
                        if (!TextUtils.isEmpty(result)) {
                            goActivity(result);


                        } else {
                            ToastUtils.showToast(getString(R.string.invalid_QR_code));
                            toFinish();

                        }

                    }

                    @Override
                    public void onAnalyzeFailed() {
                        ToastUtils.showToast(getString(R.string.invalid_QR_code));
                        toFinish();


                    }
                });

            }
        }
    }


    public void toFinish() {
        finish();
    }

    public void goActivity(String result) {
        Intent intent = new Intent(ScanHomeActivity.this, HomeActivity.class);
        intent.putExtra("userName", result);
        setResult(RESULT_OK, intent);
        finish();
    }


    @Override
    public void obtainScanSuccess(JSONObject jsonObject) {


    }


}
